package com.haiheng.voiceandbook.api

import android.util.JsonReader
import android.util.Log
import com.google.gson.JsonObject
import com.haiheng.voiceandbook.BuildConfig
import com.haiheng.voiceandbook.Constants
import com.haiheng.voiceandbook.commo.CategoriesSettingDataDto
import com.haiheng.voiceandbook.commo.VersionInfoDto
import com.haiheng.voiceandbook.common.*
import com.haiheng.voiceandbook.interceptor.HeadInterceptor
import com.haiheng.voiceandbook.interceptor.HttpBaseParamsLoggingInterceptor
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.*
import java.util.concurrent.TimeUnit


//ACS平台
interface ApiService {
    companion object Factory {
        val apiService = create()

        fun create(): ApiService {
            val okHttpClient = OkHttpClient.Builder()
                .addInterceptor(HeadInterceptor())
                .addInterceptor(HttpBaseParamsLoggingInterceptor.Builder()
                    .addParam("appId",Constants.getAppId())
                    .addParam("versionName",BuildConfig.VERSION_NAME)
                    .addParam("versionCode",BuildConfig.VERSION_CODE.toString())
                    .addParam("devicesId",Constants.getDevicesId().toString())
                    .build())
                .addInterceptor(HttpLoggingInterceptor { //访问网络请求，和服务端响应请求时。将数据拦截并输出
//                    Log.d("KotlinApiService", "RetrofitHelper-Http: $it")
                }.setLevel(HttpLoggingInterceptor.Level.BODY)) //Log等级
                .connectTimeout(16, TimeUnit.SECONDS).build()
            val retrofit = Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(CoroutineCallAdapterFactory())
                .baseUrl(Constants.BASE_URL)
                .build()
            return retrofit.create(ApiService::class.java)
        }
    }

    /**
     * 获取图书分类
     */
    @GET("v1/ebook/categories")
    suspend fun categories(): BaseDto<CategoriesDataDto>

    /**
     * 通过分类获取书籍信息
     */
    @GET("v1/ebook/books/{categoryId}")
    suspend fun booksByCategoryId(
        @Path("categoryId") categoryId: Long,
        @Query("page") page: Int,
        @Query("rows") rows: Int
    ): BaseDto<BookMainDto>

    /**
     * 获取书籍详细信息
     */
    @GET("v1/ebook/book/{bookId}")
    suspend fun getBookDetail(@Path("bookId") bookId: Long,
                              @Query("libraryNo") libraryNo: String,
                              @Query("terminalNo") terminalNo: String): BaseDto<BookDetailDataDto>


    /**
     * 搜索书籍
     */
    @POST("v1/ebook/book/search")
    suspend fun search(@Body vo: SearchVo): BaseDto<BookMainDto>


    /**
     * 获取最新排行
     */
    @GET("v1/ebook/latestRank")
    suspend fun latestRank(@Query("page") page: Int, @Query("rows") rows: Int): AmountDataDto


    /**
     * 每日推荐
     */
    @GET("v1/ebook/daily/recommend")
    suspend fun recommend(@Query("page") page: Int, @Query("rows") rows: Int): AmountDataDto


    /**
     * 获取热门排行
     */
    @GET("v1/ebook/hotRank")
    suspend fun hotRank(@Query("page") page: Int, @Query("rows") rows: Int): AmountDataDto

    /**
     * 查看听书配置
     */
    @GET("v1/xmly/settting/listen/book")
    suspend fun getSettingCategory(@Query("libraryNo") libraryNo: String)
    : BaseDto<CategoriesSettingDataDto>

    /**
     * 获取版本信息
     */
    @GET("/hh_pad/apk-version/latest")
    suspend fun getVersionInfo(@Query("verNo") verNo: String,@Query("appName")appName:String):BaseDto<VersionInfoDto>

    @GET
    fun getVersionConfig(@Url url:String): Call<JsonObject>


    /**
     * 上报专辑点击量
     */
    @GET("v1/xmly/report/click")
    suspend fun reportAlbumClick(
        @Query("libraryNo") libraryNo: String,
        @Query("terminalNo") terminalNo: String,
        @Query("albumId") albumId: String
    ): BaseDto<Any>



    /**
     * 上报播放页面停留时长
     */
    @GET("v1/xmly/report/play")
    suspend fun reportPlay(
        @Query("libraryNo") libraryNo: String,
        @Query("terminalNo") terminalNo: String,//设备编号,设备端必填
        @Query("albumId") albumId: String,
        @Query("timer") timer: String,//持续时长
    ): BaseDto<Any>

    /**
     * 用户点击开始阅读后，在播放页面生成定时器,退出后上报时长
     */
    @GET("v1/ebook/report/read/duration")
    suspend fun reportReading(
        @Query("libraryNo") libraryNo: String,
        @Query("terminalNo") terminalNo: String,//设备编号,设备端必填
        @Query("bookId") bookId: String,
        @Query("timer") timer: String,
    ): BaseDto<Any>

    /**
     * 阅读次数上报
     */
    @GET("v1/ebook/report/read/times")
    suspend fun reportReadTimes(
        @Query("libraryNo") libraryNo: String,
        @Query("terminalNo") terminalNo: String,//设备编号,设备端必填
        @Query("bookId") bookId: String,
    ): BaseDto<Any>
}